package com.masterclient.app.modules.aftersale;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.modules.aftersale.contract.AddReturnLogistContract;
import com.masterclient.app.modules.aftersale.contract.AddReturnLogistPresenter;
import com.masterclient.app.widget.TitlebarView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.masterclient.app.R.id.tv_reason;

/**
 * 作者：sean
 * 创建时间：2017/8/27 15:43
 * 功能说明：
 */

public class AddReturnLogistActivity extends BaseActivity<AddReturnLogistPresenter> implements AddReturnLogistContract.View {

    @BindView(R.id.title_bar)
    TitlebarView titleBar;
    @BindView(tv_reason)
    TextView tvReason;
    @BindView(R.id.rl_choose)
    RelativeLayout rlChoose;
    @BindView(R.id.et_no)
    EditText etNo;
//    @BindView(R.id.et_phone)
//    EditText etPhone;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list;
    private ShipmentDialog shipmentDialog;
    private String companyName,code,type;
    private String shipment_id;
    private String refund_id;//订单修改物流是order_express_id
    private ProgressLoginDialog progressDialog;
//    private String phoneStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addlogist);
        ButterKnife.bind(this);

        progressDialog = new ProgressLoginDialog(getActivity());

        titleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        refund_id = getBundleString("refund_id");
        if ("1".equals(getBundleString("update"))){
            companyName = getBundleString("shipment_company");
            shipment_id = getBundleString("track_no");
            code = getBundleString("shipment_code");
            etNo.setText(shipment_id);
            tvReason.setText(companyName);
            tvReason.setTextColor(Color.parseColor("#141922"));
        }
        type = getBundleString("type");
        if (TextUtils.isEmpty(type)) type = "";

        if ("order".equals(type)){
            titleBar.setTitle("修改物流");
            tvConfirm.setBackgroundResource(R.drawable.shape_round_ff6809);
        }else if ("purchase_after_sale".equals(type)){
            titleBar.setTitle("填写物流信息");
            tvConfirm.setBackgroundResource(R.drawable.shape_round_ff6809);
        }
        mPresenter.getShipmentItems();
    }

    @OnClick({R.id.tv_confirm,R.id.rl_choose})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                if(TextUtils.isEmpty(companyName)){
                    showToast("请选择快递公司");
                    return;
                }
                shipment_id = etNo.getText().toString().trim();
                if(TextUtils.isEmpty(shipment_id)){
                    showToast("请填写物流单号");
                    return;
                }
//                phoneStr = etPhone.getText().toString().trim();
//                if(TextUtils.isEmpty(phoneStr)){
//                    showToast("请填写手机号");
//                    return;
//                }
                progressDialog.show();
                progressDialog.setDesc("加载中...");
                    if ("order".equals(type)) {
                        mPresenter.orderShipment(refund_id, shipment_id, code);
                    }else if ("purchase".equals(type)){
                        mPresenter.setAfterSalesPurchaseShipment(refund_id,companyName,code,shipment_id);
                    }else {

                    mPresenter.setAfterSalesShipment(companyName, refund_id, shipment_id, code);
                }
                break;
            case R.id.rl_choose:
                if (list == null){
                    return;
                }
                ARouter.getInstance().build(AroutePath.LOGISTICS_COMPANY_ACTIVITY)
                        .withSerializable("list",(Serializable)list)
                        .navigation(this,0x117);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x117&&resultCode == RESULT_OK){
            ShipmentsBean.DataBean.ShShipmentCompanysBean bean = (ShipmentsBean.DataBean.ShShipmentCompanysBean) data.getSerializableExtra("bean");
            companyName = bean.getSh_name();
            AddReturnLogistActivity.this.code = bean.getSh_code();
            tvReason.setText(companyName);
            tvReason.setTextColor(Color.parseColor("#141922"));
        }
    }

    @Override
    protected void onPause() {
//        hideSoftInput(etPhone);
        super.onPause();
    }
    @Override
    public void showShipmnets(List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list) {
        this.list = list;
    }

    @Override
    public void showSuccess(String msg) {
        progressDialog.dismiss();
        if (!"0".equals(msg)){
            showToast(msg);
            finish();
        }
    }

    @Override
    public void showResult(boolean isSuccess, String msg) {
        progressDialog.dismiss();
        ToastUtil.showToast(msg);
        if (isSuccess) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
